package controllers;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.rosaloves.bitlyj.Url;
import static com.rosaloves.bitlyj.Bitly.*;

import org.codehaus.jackson.node.ObjectNode;
import org.tapaha.util.CharEncoding;
import org.tapaha.util.DateCalculator;

import com.avaje.ebean.SqlRow;

import models.Article;
import play.*;
import play.mvc.*;
import play.mvc.BodyParser.Json;

import play.api.libs.json.*;
import views.html.*;

public class Application extends Controller {
	
  public static Result index() {
	String today_date = DateCalculator.getDate("yyyy-MM-dd");
    return ok(
    	index.render(
    		"Your new application is ready.", 
    		Article.getList(today_date), 
    		today_date,
    		DateCalculator.getYesterdayDate(today_date),
    		"null",
    		DateCalculator.getDate("yyyy-MM-dd")
    	)
    );
  }
  
  public static Result list(String req_date) {
    return ok(
    	index.render(
    		"Your new application is ready.",
    		Article.getList(req_date),
    		req_date,
    		DateCalculator.getYesterdayDate(req_date),
    		DateCalculator.getTomorrowDate(req_date),
    		DateCalculator.getDate("yyyy-MM-dd")
    	)
    );
  }
  
  public static Result view(Integer issue_id) {
	return ok(view.render(issue_id));
  }
  
  @BodyParser.Of(Json.class)
  public static Result viewJSON(Integer issue_id) {
	SqlRow sr = Article.getArticle(issue_id);
	ObjectNode result = play.libs.Json.newObject();
	result.put("status", "ok");
	result.put("title", CharEncoding.decode(sr.getString("title"), "UTF-8"));
	result.put("content", CharEncoding.decode(sr.getString("content"), "UTF-8"));
	result.put("article_no", sr.getInteger("article_no"));
	
	return ok(result);
  }
  
  @BodyParser.Of(Json.class)
  public static Result shorten(String long_url) {
	ObjectNode result = play.libs.Json.newObject();
//	Url url = as("scrawle", "8f2de4810866ea95cfc861f01c9138eee5866183").call(shorten(long_url));
//	result.put("short_url", url.getShortUrl());
	
	return ok(result);
  }
  
  public static Result getCommentHTML(String target_url) {
	  URL url = null;
	  HttpURLConnection httpUrlConnection = null;
	  StringBuffer sbContent = new StringBuffer();
	  String line = "";
	  
	  try {
		  url = new URL(target_url);
		  httpUrlConnection = (HttpURLConnection)url.openConnection();
		  httpUrlConnection.setReadTimeout(120*10);
		  
		  if(httpUrlConnection.getResponseCode() == 200) {
			  BufferedReader reader = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
			  
			  while((line = reader.readLine()) != null) {
				  sbContent.append(line);
			  }
		  }
	  } catch (Exception e) {
		  e.printStackTrace();
	  } finally {
		  httpUrlConnection.disconnect();
	  }
	  
//	  return ok(proxy.render(sbContent.toString()));
	  return ok(sbContent.toString());
  }
  
  public static Result getComment(String id, String no, Integer sno, Integer spl, Integer mno, Integer article_no) {
	  URL url = null;
	  HttpURLConnection httpUrlConnection = null;
	  StringBuffer sbContent = new StringBuffer();
	  String line = "";
	  
	  String target_url = "http://www.slrclub.com/bbs/comment_db/load.php";
	  
	  try {
		  url = new URL(target_url);
		  httpUrlConnection = (HttpURLConnection)url.openConnection();
		  httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		  httpUrlConnection.setRequestProperty("Referer", "http://www.slrclub.com/bbs/vx2.php?id=free&no=" + article_no);
		  httpUrlConnection.setRequestMethod("POST");
		  httpUrlConnection.setDoOutput(true);
		  httpUrlConnection.setReadTimeout(120*10);
		  
//		  String param = "mno=9&sno=1&spl=50&no=o03n2&id=free";
		  String param = "id=" + id + "&no=" + no + "&sno=" + sno + "&spl=200&mno=" + mno; 
		  
		  OutputStream out_stream = httpUrlConnection.getOutputStream();
	
		  out_stream.write(param.getBytes("UTF-8"));
		  out_stream.flush();
		  out_stream.close();
		  
		  if(httpUrlConnection.getResponseCode() == 200) {
			  BufferedReader reader = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
			  
			  while((line = reader.readLine()) != null) {
				  sbContent.append(line);
			  }
		  }
	  } catch (Exception e) {
		  e.printStackTrace();
	  } finally {
		  httpUrlConnection.disconnect();
	  }
	  
//	  return ok(proxy.render(sbContent.toString()));
	  return ok(sbContent.toString());
  }
  
  public static Result changelog() {
	  return ok(changelog.render());
  }
}
